﻿using System.Collections.Generic;
using System.Data;

namespace net.entity.framework.db {
    internal static class Repository {
        internal static object ExecuteScalar(DataProvider provider, string commandText) {
            object value;
            using (var connection = provider.GetConnection(true)) {
                connection.Open();
                using (var command = connection.CreateCommand()) {
                    command.Connection = connection;
                    command.CommandText = commandText;
                    value = command.ExecuteScalar();
                }
                connection.Close();
            }
            return value;
        }

        //public DbDataReader ExecuteReader(string commandText) {
        //    using (connection = GetConnection()) {
        //        using (command = GetCommand()) {
        //            connection.Open();
        //            command.Connection = connection;
        //            command.CommandText = commandText;
        //            var reader = command.ExecuteReader();
        //            try {
        //                while (reader.Read()) {
        //                    var val1 = reader.GetValue(0);
        //                    Coding.DoNothingWith(val1);
        //                }
        //            } finally {
        //                reader.Close();
        //            }
        //            return command.ExecuteReader();
        //        }
        //    }
        //}

        internal static DataSet ExecuteQuery(DataProvider provider, string commandText) {
            var ds = new DataSet();
            using (var connection = provider.GetConnection(true)) {
                connection.Open();
                using (var dataAdapter = provider.GetDataAdapter(commandText)) {
                    dataAdapter.Fill(ds);
                }
                connection.Close();
            }
            return ds;
        }

        internal static DataRow ExecuteSingle(DataProvider provider, string commandText) {
            var ds = new DataSet();
            using (var connection = provider.GetConnection(true)) {
                connection.Open();
                using (var dataAdapter = provider.GetDataAdapter(commandText)) {
                    dataAdapter.Fill(ds);
                }
                connection.Close();
            }
            return ds.Tables[0].Rows[0];
        }

        internal static int ExecuteNonQuery(DataProvider provider, string commandText) {
            int result;
            using (var connection = provider.GetConnection(true)) {
                connection.Open();
                using (var transaction = provider.GetTransaction(true)) {
                    result = ExecuteNonQuery(connection, transaction, commandText);
                    transaction.Commit();
                }
                connection.Close();
            }
            return result;
        }

        internal static int ExecuteNonQuery(IDbConnection connection, IDbTransaction transaction, string commandText) {
            int result;
            using (var command = connection.CreateCommand()) {
                command.Transaction = transaction;
                command.Connection = connection;
                command.CommandText = commandText;
                result = command.ExecuteNonQuery();
            }
            return result;
        }

        //internal static void ExecuteBatchQuery(DataProvider provider, List<string> commandTexts) {
        //    using (var connection = provider.GetConnection(true)) {
        //        connection.Open();
        //        using (var trans = provider.GetTransaction(true)) {
        //            foreach (var commandText in commandTexts) {
        //                if (commandText.IsNotNullOrEmpty()) {
        //                    using (var command = connection.CreateCommand()) {
        //                        command.Transaction = trans;
        //                        command.CommandText = commandText;
        //                        command.ExecuteNonQuery();
        //                    }
        //                }
        //            }
        //            trans.Commit();
        //        }
        //        connection.Close();
        //    }
        //}

        internal static void Commit(DataProvider provider, List<DataTable> dataTables) {
            using (var connection = provider.GetConnection(true)) {
                connection.Open();
                using (var transaction = provider.GetTransaction(true)) {
                    foreach (var dataTable in dataTables) {
                        using (var tempTable = new TempTable(dataTable, provider)) {
                            tempTable.UpdateDestination();
                        }
                    }
                    transaction.Commit();
                }
                connection.Close();
            }
        }
    }
}